<?php

namespace app\home\service;

use think\Db;
use think\Controller;
use OSS\OssClient;

class ApiSellService
{

    /**
     * 生成随机字符串
     */
    static public function getRandChar($length = 8, $is_str = true)
    {
        $str = null;
        $strPol = "0123456789";
        if ($is_str) {
            $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        }
        $max = strlen($strPol) - 1;
        for ($i = 0; $i < $length; $i++) {
            $str .= $strPol[rand(0, $max)];
        }
        return $str;
    }

    /**
    * 密码登录
    */
    static public function pass_login($phone, $password)
    {
        if(!$phone){
            return array('code'=>201, 'msg'=>'请输入手机号');
        }
        //检测用户是否存在
        $sell = Db::name('sell')->where('phone', $phone)->find();
        if(!$sell){
            return array('code'=>201, 'msg'=>'账号不存在');
        }
        if(!$password){
            return array('code'=>201, 'msg'=>'请输入密码');
        }
        //验证密码
        $password = md5(md5($password.$sell['sale']));
        if($password !== $sell['password']){
            return array('code'=>201, 'msg'=>'密码输入有误');
        }
        //生成token
        $token = self::getRandChar(50);
        //数据处理
        $data = array();
        $data['token'] = $token;
        $data['createtime'] = time();
        $data['expiretime'] = strtotime("+7 day");
        $token_count = Db::name('sell_token')->where('sell_id', $sell['id'])->count();
        $result = 0;
        if ($token_count) {
            $result = Db::name('sell_token')->where('sell_id', $sell['id'])->update($data); 
        } else {
            $data['sell_id']  = $sell['id'];
            $result = Db::name('sell_token')->insert($data); 
        }
        if($result < 1){
            return array('code'=>201, 'msg'=>'登录失败');
        }
        //返回数据
        $data = array();
        $data['token'] = $token;
        $sellsite = Db::name('sellsite')->where('id', $sell['sellsite_id'])->find();
        $data['sellsite_name'] = $sellsite['name'];
        $data['phone'] = $sell['phone'];
        $data['name'] = $sell['name'];
        if ($sell['image']) {
            $data['image'] = 'http://'.$_SERVER['HTTP_HOST'].$sell['image'];
        } else {
            $data['image'] = 'http://'.$_SERVER['HTTP_HOST'].'/uploads/sell/default.png';
        }

        return array('code'=>200, 'msg'=>'登录成功', 'data'=>$data);
    }

    /**
    * 验证会员token是否有效
    */
    static public function check_token($token)
    {   
        $token = Db::name('sell_token')->where('token', $token)->find();
        if(!$token){
            return array('code'=>202,'msg'=>'token无效');
        }
        if($token['expiretime'] < time()){
            return array('code'=>202,'msg'=>'token过期');
        }
        $sell = Db::name('sell')->where('id', $token['sell_id'])->find();
        if (!$sell) {
            return array('code'=>202,'msg'=>'token无效');
        }
        
        return $token;
    }

    /**
    * 退出
    */
    static public function logout($token)
    {
        if(!$token){
            return array('code'=>201, 'msg'=>'token不能为空');
        }
        //检测token是否有效
        $sell_token = self::check_token($token);
        if(isset($sell_token['code'])){
            return $sell_token;
        }
        //执行删除用户token 
        $result = Db::name('sell_token')->where('token', $token)->delete();
        
        if(!$result){
            return array('code'=>201, 'msg'=>'退出失败');
        }

        return array('code'=>200, 'msg'=>'退出成功');
    }

    /**
    * 手机验证码登录
    */
    static public function phone_login($phone, $code)
    {
        if(!$phone){
            return array('code'=>201, 'msg'=>'请输入手机号');
        }
        if(!$code){
            return array('code'=>201, 'msg'=>'请输入验证码');
        }
        //检测用户是否存在
        $sell = Db::name('sell')->where('phone', $phone)->find();
        if(!$sell){
            return array('code'=>201, 'msg'=>'账号不存在');
        }
        //验证手机验证码
        $sell_code = Db::name('sell_code')->where('phone', $phone)->find();
        if(!$sell_code){
            return array('code'=>201, 'msg'=>'请先发送验证码');
        }
        //检测验证码是否正确
        if($code !== $sell_code['code']){
            return array('code'=>201, 'msg'=>'手机验证码错误');
        }
        //检测验证码是否过期
        $time = time();
        if($time > $sell_code['expiretime']){
            return array('code'=>201, 'msg'=>'验证码已过期，请重新获取');
        }
        //生成token
        $token = self::getRandChar(50);
        //数据处理
        $data = array();
        $data['token'] = $token;
        $data['createtime'] = time();
        $data['expiretime'] = strtotime("+7 day");
        $token_count = Db::name('sell_token')->where('sell_id', $sell['id'])->count();
        $result = 0;
        if ($token_count) {
            $result = Db::name('sell_token')->where('sell_id', $sell['id'])->update($data); 
        } else {
            $data['sell_id']  = $sell['id'];
            $result = Db::name('sell_token')->insert($data); 
        }
        if($result < 1){
            return array('code'=>201, 'msg'=>'登录失败');
        }
        //删除验证码
        Db::name('sell_code')->where('phone', $phone)->delete();
        //返回数据
        $data = array();
        $data['token'] = $token;
        $sellsite = Db::name('sellsite')->where('id', $sell['sellsite_id'])->find();
        $data['sellsite_name'] = $sellsite['name'];
        $data['phone'] = $sell['phone'];
        $data['name'] = $sell['name'];
        if ($sell['image']) {
            $data['image'] = 'http://'.$_SERVER['HTTP_HOST'].$sell['image'];
        } else {
            $data['image'] = 'http://'.$_SERVER['HTTP_HOST'].'/uploads/sell/default.png';
        }

        return array('code'=>200, 'msg'=>'登录成功', 'data'=>$data);
    }

    /**
     * 发送手机验证码
     */
    static public function send_code($phone)
    {
        if(!$phone){
            return array('code'=>201, 'msg'=>'请输入手机号');
        }
        $phone = base64_decode($phone);
        $phone = str_replace('zuche', '', $phone);
        $phone = base64_decode($phone);
        $res = self::check_phone($phone);
        if (isset($res['code'])) {
            return $res;
        }
        //手机号是否已经注册
        $count = Db::name('sell')->where('phone', $phone)->count();
        if ($count < 1) {
            return array('code'=>201, 'msg'=>'请先注册');
        }
        //发送短信
        $smsapi = "http://www.smsbao.com/"; //短信网关
        $user = "zhaojincong"; //短信平台帐号
        $pass = md5("dc123."); //短信平台密码
        $random = self::getRandChar(6, false);
        $content = "【尚博德睿】您的验证码是：{$random}，5分钟内有效，如非本人操作，请忽略本短信！";//要发送的短信内容
        $phone = $phone;
        $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
        $result = file_get_contents($sendurl);
        if ($result !== '0') {
            return array('code'=>201, 'msg'=>'网络异常');
        }
        //处理数据
        $data = array();
        $data['code'] = $random;
        $data['createtime'] = time();
        $data['expiretime'] = time() + 5*60;
        $count = Db::name('sell_code')->where('phone', $phone)->count();
        if ($count < 1) {
            $data['phone'] = $phone;
            Db::name('sell_code')->insert($data);
        } else {
            Db::name('sell_code')->where('phone', $phone)->update($data);
        }

        return array('code'=>200, 'msg'=>'发送成功');
    }

    /**
     * 验证手机号格式
     */
    static public function check_phone($phone)
    {
        if (!preg_match("/^(13|14|15|17|18|19)(\d{9})$/i", $phone)) {
            return array('code'=>201, 'msg'=>'手机号格式有误');
        }
        return true;
    }

    /**
    * 忘记密码
    */
    static public function forget_pass($phone, $code, $newpassword, $repassword)
    {
        if(!$phone){
            return array('code'=>201, 'msg'=>'请输入手机号');
        }
        //检测手机是否已经注册
        $sell = Db::name('sell')->where('phone', $phone)->find();
        if(!$sell){
            return array('code'=>201, 'msg'=>'账号不存在');
        }
        if(!$code){
            return array('code'=>201, 'msg'=>'请输入验证码');
        }
        //检测验证码是否正确,并且检测验证码是否过期
        $sell_code = Db::name('sell_code')->where('phone', $phone)->find();
        if(!$sell_code){
            return array('code'=>201, 'msg'=>'请先发送验证码');
        }
        //检测验证码是否正确
        if($code !== $sell_code['code']){
            return array('code'=>201, 'msg'=>'手机验证码错误');
        }
        //检测验证码是否过期
        $time = time();
        if($time > $sell_code['expiretime']){
            return array('code'=>201, 'msg'=>'验证码已过期,请重新发送');
        }
        if(!$newpassword){
            return array('code'=>201, 'msg'=>'请输入新密码');
        }
        if(!$repassword){
            return array('code'=>201, 'msg'=>'请再次输入密码');
        }
        if($newpassword !== $repassword){
            return array('code'=>201, 'msg'=>'两次密码输入不一致');
        }
        //检测新密码是否符合密码规则
        if (strlen($newpassword)<6 || strlen($newpassword)>20) {
            return array('code'=>201, 'msg'=>'密码长度为6-20位');
        }
        // 生成密码盐
        $sale = self::getRandChar();
        //封装修改条件及修改的数据
        $data = array();
        $data['password'] = md5(md5($newpassword.$sale));  
        $data['sale'] = $sale;
        //执行修改密码 
        $result = Db::name('sell')->where('phone', $phone)->update($data);
        if (!$result) {
            return array('code'=>201, 'msg'=>'修改失败');
        }
        //删除验证码
        Db::name('sell_code')->where('phone', $phone)->delete();

        return array('code'=>200, 'msg'=>'修改成功');
    }

    /**
    * 修改密码
    */
    static public function change_pass($token, $old_pass, $new_pass, $re_pass)
    {
        if (!$token) {
            return array('code'=>201, 'msg'=>'token不能为空');
        }
        //检测token是否有效
        $sell_token = self::check_token($token);
        if(isset($sell_token['code'])){
            return $sell_token;
        }
        if (!$old_pass) {
            return array('code'=>201, 'msg'=>'请输入旧密码');
        }
        if (!$new_pass) {
            return array('code'=>201, 'msg'=>'请输入新密码');
        }
        if (!$re_pass) {
            return array('code'=>201, 'msg'=>'请再次输入密码');
        }
        //查询修改密码的用户
        $sell = Db::name('sell')->where('id', $sell_token['sell_id'])->find();
        //检测原始密码是否正确
        $password = md5(md5($old_pass.$sell['sale']));
        if($password !== $sell['password']){
            return array('code'=>201, 'msg'=>'旧密码错误');
        }
        if($old_pass == $new_pass){
            return array('code'=>201, 'msg'=>'旧密码和新密码不能相同');
        }
        if($new_pass !== $re_pass){
            return array('code'=>201, 'msg'=>'新密码和重复密码不相同');
        }

        //检测新密码是否符合密码规则
        if (strlen($new_pass)<6 || strlen($new_pass)>20) {
            return array('code'=>201, 'msg'=>'密码长度为6-20位');
        }
        // 生成密码盐
        $sale = self::getRandChar();

        //封装修改条件及修改的数据
        $where = array();
        $where['id'] = $sell['id'];
        $data = array();
        $data['password'] = md5(md5($new_pass.$sale));  
        $data['sale'] = $sale;  
        //执行修改密码 
        $result = Db::name('sell')->where($where)->update($data);
        if(!$result){
            return array('code'=>201, 'msg'=>'网络异常');
        }
        
        return array('code'=>200, 'msg'=>'密码修改成功');
    }

    /**
    * 修改头像
    */
    static public function upload_img($token, $file)
    {
        if (!$token) {
            return array('code'=>201, 'msg'=>'token不能为空');
        }
        //检测token是否有效
        $sell_token = self::check_token($token);
        if(isset($sell_token['code'])){
            return $sell_token;
        }
        $sell_id = $sell_token['sell_id'];
        if (!$file) {
            return array('code'=>201, 'msg'=>'请上传头像');
        }
        
        $file1 = self::upload_image($file);
        //执行修改
        $data = array();
        $data['image'] = $file1['data']['path'];
        $result = Db::name('sell')->where('id', $sell_id)->update($data);
        $data['image'] = 'http://'.$_SERVER['HTTP_HOST'].$data['image'];
        
        return array('code'=>200, 'msg'=>'修改成功', 'data'=>$data);
    }

    /**
     * 上传单图
     */
    static public  function upload_image($file)
    {
        $filePath = 'sell';
        $filePaths = ROOT_PATH . 'public' . DS . 'uploads' . DS .$filePath;
        if (!file_exists($filePaths)) {
            mkdir($filePaths, 0777, true);
        }
        $info = $file->move($filePaths,self::create_file_name(20));
        if ($info) {
            $img_name = $info->getSaveName();
            $path = '/uploads/'.$filePath.'/'.$img_name;
            $data = array();
            $data['path'] = $path;
            return array('code'=>200, 'msg'=>'成功', 'data'=>$data);
        } else {
            // 上传失败获取错误信息
            // return $file->getError();
            return array('code'=>201, 'msg'=>'失败');
        }
    }

    /**
     * 生成文件名称
     */
    static function create_file_name($length)
    {
        $str = '0123456789abcdefghijklmnopqrstuvwxyz';
        $len = strlen($str) - 1;
        $randstr = '';
        for ($i = 0; $i < $length; $i++){
            $num = mt_rand(0, $len);
            $randstr .= $str[$num];
        }
        return $randstr;
    }

    /**
    * 订单列表
    */
    static public function order_list($token, $page)
    {
        //判断登录
        if(!$token){
            return array('code'=>201, 'msg'=>'token不能为空');
        }
        //检测token是否有效
        $sell_token = self::check_token($token);
        if(isset($sell_token['code'])){
            return $sell_token;
        }
        $sell_id = $sell_token['sell_id'];

        if (!$page) {
            $page = 1;
        }
        $data = array();
        $num = 6;
        $start_num = ($page-1)*$num;
        $where = array();
        $where['sell_id'] = $sell_id;
        $where['order_status'] = array('>', 1);
        $count = Db::name('order')->where($where)->count();
        $data['total_page'] = ceil($count/$num);
        $order = Db::name('order')->where($where)->field('id, order_sn, rent_name, rent_price, cash_price, total_price, order_status, createtime, type, if_cash')->limit($start_num, $num)->order('id desc')->select();
        foreach ($order as $key => $value) {
            $order[$key]['createtime'] = date('Y-m-d H:i', $value['createtime']);
            $order[$key]['type'] = $value['type'];
            $order[$key]['if_cash'] = 1;
            $order[$key]['if_upload'] = 1;
            //订单状态:0=已取消,1=待支付,2=提车审核,3=审核通过,4=使用中,5=归还申请,6=退车中,7=已完成
            if ($value['order_status'] == 0) {
                $order[$key]['status_name'] = '已取消';
            }
            if ($value['order_status'] == 1) {
                $order[$key]['status_name'] = '待支付';
            }
            if ($value['order_status'] == 2) {
                $order[$key]['status_name'] = '提车审核中';
            }
            if ($value['order_status'] == 3) {
                $order[$key]['status_name'] = '审核通过';
            }
            if ($value['order_status'] == 4) {
                $order[$key]['status_name'] = '使用中';
                if ($value['if_cash'] == 0) {
                    $order[$key]['if_cash'] = 0;
                }
            }
            if ($value['order_status'] == 5) {
                $order[$key]['status_name'] = '退车申请';
            }
            if ($value['order_status'] == 6) {
                $order[$key]['status_name'] = '退车中';
                $rent_info = Db::name('rent_info')->where(array('type'=>2, 'order_id'=>$value['id']))->find();
                if (!$rent_info) {
                    $order[$key]['if_upload'] = 0;
                }
            }
            if ($value['order_status'] == 7) {
                $order[$key]['status_name'] = '已完成';
            }
        }
        $data['order'] = $order;
        return array('code'=>200, 'msg'=>'成功', 'data'=>$data);
    }

    /**
    * 订单详情
    */
    static public function order_detail($token, $order_id)
    {
        //判断登录
        if(!$token){
            return array('code'=>201, 'msg'=>'token不能为空');
        }
        //检测token是否有效
        $sell_token = self::check_token($token);
        if(isset($sell_token['code'])){
            return $sell_token;
        }
        $sell_id = $sell_token['sell_id'];
        if (!$order_id) {
            return array('code'=>201, 'msg'=>'请选择要查看的订单');
        }
        $where = array();
        $where['id'] = $order_id;
        $where['order_status'] = array('>', 1);
        $order = Db::name('order')->where($where)->find();
        if (!$order) {
            return array('code'=>201, 'msg'=>'订单异常');
        }
        //订单状态:0=已取消,1=待支付,2=提车审核,3=审核通过,4=使用中,5=归还申请,6=退车中,7=已完成
        if ($order['order_status'] == 0) {
            $order['order_status'] = '已取消';
        }
        if ($order['order_status'] == 1) {
            $order['order_status'] = '待支付';
        }
        if ($order['order_status'] == 2) {
            $order['order_status'] = '提车审核中';
        }
        if ($order['order_status'] == 3) {
            $order['order_status'] = '审核通过';
        }
        if ($order['order_status'] == 4) {
            $order['order_status'] = '使用中';
        }
        if ($order['order_status'] == 5) {
            $order['order_status'] = '退车申请';
        }
        if ($order['order_status'] == 6) {
            $order['order_status'] = '退车中';
        }
        if ($order['order_status'] == 7) {
            $order['order_status'] = '已完成';
        }
        if (!$order['payment_code']) {
            $order['payment_code'] = '无';
        }
        if ($order['payment_code'] == 1) {
            $order['payment_code'] = '微信';
        }
        if ($order['payment_code'] == 2) {
            $order['payment_code'] = '支付宝';
        }
        $order['createtime'] = date('Y-m-d H:i', $order['createtime']);
        if ($order['start_time']) {
            $order['start_time'] = date('Y-m-d H:i', $order['start_time']);
        } else {
            $order['start_time'] = '';
        }
        if ($order['return_time']) {
            $order['return_time'] = date('Y-m-d H:i', $order['return_time']);
        } else {
            $order['return_time'] = '';
        }
        if ($order['expire_time']) {
            $order['expire_time'] = date('Y-m-d H:i', $order['expire_time']);
        } else {
            $order['expire_time'] = '';
        }
        if (!$order['card_number']) {
            $order['card_number'] = '';
        }
        
        $data = array();
        $data['order'] = $order;
        return array('code'=>200, 'msg'=>'成功', 'data'=>$data);
    }

    /**
    * 交易额列表
    */
    static public function transaction_list($token, $sellsite_id)
    {
        //判断登录
        if(!$token){
            return array('code'=>201, 'msg'=>'token不能为空');
        }
        //检测token是否有效
        $sell_token = self::check_token($token);
        if(isset($sell_token['code'])){
            return $sell_token;
        }
        $sell_id = $sell_token['sell_id'];

        $data = array();
        $data['sellsite_list'] = Db::name('sellsite')->select();
        $data['sell_list'] = array();
        if ($data['sellsite_list']) {
            if (!$sellsite_id) {
                $sellsite_id = $data['sellsite_list'][0]['id'];
            } else {
                //判断站点是否存在
                $sellsite = Db::name('sellsite')->where('id', $sellsite_id)->find();
                if (!$sellsite) {
                    $sellsite_id = $data['sellsite_list'][0]['id'];
                }
            }
            //获取业务员列表
            $sell = Db::name('sell')->where('sellsite_id', $sellsite_id)->select();
            if ($sell) {
                foreach ($sell as $key => $value) {
                    //获取交易额
                    // //本月开始
                    // $begin_month = date('Y-m-01 00:00:00');
                    // $begin_month = strtotime($begin_month);
                    // //本月结束
                    // $end_month = date('Y-m-d 23:59:59', strtotime('Last day of this month'));
                    // $end_month = strtotime($end_month);
                    $where = array();
                    $where['sell_id'] = $value['id'];
                    $where['order_status'] = array('>', 3);
                    $order = Db::name('order')->where($where)->select();
                    $total_price = 0;
                    $total_count = 0;
                    if ($order) {
                        foreach ($order as $k => $v) {
                            $total_price += $v['rent_price'];
                        }
                        $total_count = count($order);
                    }

                    $data['sell_list'][$key]['name'] = $value['name'];
                    $data['sell_list'][$key]['total_price'] = $total_price;
                    $data['sell_list'][$key]['total_count'] = $total_count;
                    $data['sell_list'][$key]['practical_price'] = $total_price;
                }
            }
        }

        return array('code'=>200, 'msg'=>'成功', 'data'=>$data);
    }

    /**
    * 个人交易额信息
    */
    static public function sell_info($token)
    {
        //判断登录
        if(!$token){
            return array('code'=>201, 'msg'=>'token不能为空');
        }
        //检测token是否有效
        $sell_token = self::check_token($token);
        if(isset($sell_token['code'])){
            return $sell_token;
        }
        $sell_id = $sell_token['sell_id'];

        //获取交易额
        $where = array();
        $where['sell_id'] = $sell_id;
        $where['order_status'] = array('>', 3);
        $order = Db::name('order')->where($where)->select();
        $total_price = 0;
        $total_count = 0;
        if ($order) {
            foreach ($order as $k => $v) {
                $total_price += $v['rent_price'];
            }
            $total_count = count($order);
        }

        $data['total_price'] = $total_price;
        $data['total_count'] = $total_count;
        $data['practical_price'] = $total_price;
        
        return array('code'=>200, 'msg'=>'成功', 'data'=>$data);
    }

    /**
    * 上传短视频
    */
    static public function insert_video($token, $title, $video_image, $video_file, $width, $height)
    {
        if(!$title){
            return array('code'=>201, 'msg'=>'请填写标题');
        }
        if(!$video_image){
            return array('code'=>201, 'msg'=>'请上传视频预览图');
        }
        if(!$video_file){
            return array('code'=>201, 'msg'=>'请上传视频');
        }
        if(!$width){
            $width = 16;
        }
        if(!$height){
            $height = 9;
        }
        //上传图片和视频
        $res_image = self::upload_file($video_image, 1);
        if (!$res_image) {
            return array('code'=>201, 'msg'=>'视频预览图上传失败');
        }
        $res_file = self::upload_file($video_file, 2);
        if (!$res_file) {
            return array('code'=>201, 'msg'=>'视频上传失败');
        }
        
        $data = array();
        $data['article_id'] = 1;
        $data['title'] = $title;
        $data['video_image'] = $res_image;
        $data['video_file'] = $res_file;
        $data['createtime'] = time();
        $data['width'] = $width;
        $data['height'] = $height;
        Db::name('origin_video')->insert($data);

        return array('code'=>200, 'msg'=>'发布成功');
    }

    /**
    * 上传短视频
    */
    static public function upload_file($file, $type)
    {
        $file_url = '';
        import('alioss.autoload',EXTEND_PATH);
        import('alioss.src.OSS.Core.OssException',EXTEND_PATH);

        #配置OSS基本配置
        $config = array(
            'KeyId' => 'LTAI7bHzDA02kVdo',
            'KeySecret' =>'XBV0KLSpA7Joa5G0rYsmEH1JJvJZ2T',
            'Endpoint' => 'oss-cn-shenzhen.aliyuncs.com',
            'Bucket'=> 'newnongfucang'
        );
        
        $ossClient = new OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
        if ($type == 1) {
            $getFile = $_FILES['video_image']['tmp_name'];
            #上传文件后缀
            $ext = substr($_FILES['video_image']['name'], strrpos($_FILES['video_image']['name'], '.')
             + 1);
            #重命名文件上传名字
            $dst = 'partner_image/' . md5(time()) . '.' . $ext;
        } elseif ($type == 2) {
            $getFile = $_FILES['video_file']['tmp_name'];
            #上传文件后缀
            $ext = substr($_FILES['video_file']['name'], strrpos($_FILES['video_file']['name'], '.')
             + 1);
            #重命名文件上传名字
            $dst = 'partner_video/' . md5(time()) . '.' . $ext;
        } elseif ($type == 3) {
            $getFile = $_FILES['partner_image']['tmp_name'];
            #上传文件后缀
            $ext = substr($_FILES['partner_image']['name'], strrpos($_FILES['partner_image']['name'], '.')
             + 1);
            #重命名文件上传名字
            $dst = 'partner_portrait/' . md5(time()) . '.' . $ext;
        } elseif ($type == 4) {
            $getFile = $_FILES['file']['tmp_name'];
            #上传文件后缀
            $ext = substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], '.')
             + 1);
            #重命名文件上传名字
            $dst = 'vr/' . md5(time()) . '.' . $ext;
        } else {
            return $file_url;
        }

        #执行阿里云上传
        $result = $ossClient->uploadFile($config['Bucket'], $dst, $getFile);
        if ($result) {
            //替换链接
            $file_url = str_replace("http://newnongfucang.oss-cn-shenzhen.aliyuncs.com", "https://pic.nongfucang.cn", $result['oss-request-url']);
        }
        return $file_url;
    }

}